<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="../file/images/dianmin.png" rel="shortcut icon" type="image/x-icon"/>
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="../file/css/gwc.css"/>
</head>
<body>
<div class="top">
    <img src="../file/images/dianmin.png" width="300px" height="100px">
    <div class="header-info">
        <div class="login-info" id="login_info">
            <!--用户-->
        </div>
        <div class="person-info">
            <span>|</span>
            <a href="../index.html">返回首页</a>
            <span>|</span>
            <a href="./perInfo.html">个人信息</a>
            <span>|</span>
            <a onclick="exitLogin()" style="cursor: pointer">退出登录</a>
        </div>
    </div>
</div>

<table width="95%" border="1" cellspacing="" cellpadding="5">
    <thead>
        <tr>
            <th>商品</th>
            <th>单价（元/斤）</th>
            <th>数量</th>
            <th>小计（元）</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody id="gwcTable">
        <!--内容-->
    </tbody>

    <tr>
        <td colspan="7">
            <div class="left">
                <a href="../index.html">继续购物</a>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a onclick="clearGoods()" style="cursor: pointer">清空购物车</a>
            </div>

            <div class="right" align="right">                        <!-- 结算页面 -->
                总计： <span style="color:red ;font-weight:bold" id="totalPrice">0</span>&nbsp;&nbsp;¥
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<!--                <a href="toPay.html">去结算</a>-->
<!--                <a id="toPay">去结算</a>-->
                <div id="toPayDiv" style="float: right">
                </div>
            </div>
            <div>&nbsp;</div>
        </td>
    </tr>
</table>

<script src="../file/js/jquery-3.4.1.min.js" type="text/javascript"></script>
<script src="../file/js/jquery-ui.min.js" type="text/javascript"></script>
<script src="../file/js/slide.js" type="text/javascript"></script>

<!-- 欢迎您功能  -->
<script>

    //网页一加载 就要判断用户是否登录
    $(function () {


        ifLogin();
        showAll();

        // console.log(totalPrice);
        // let str = '<a href="/toPay.html#'+totalPrice+'">去结算</a>';
        // $("#toPayDiv").html(str);
    })

    function ifLogin() {
        //开始发送请求 验证用户是否登录
        $.ajax({
            url: "/ifLogined",
            type: "POST",
            success: function (data) {
                console.log(data);
                let str = '';
                if (data.code == 200) {
                    str += '<a href="../loginPage.html" title="您好！请登录">您好！请登录</a>';
                    str += '<a href="../reg.html" title="免费注册">免费注册</a>';
                    // location.href="loginPage.html";
                    // return;
                } else {
                    str += '<a href="./perInfo.html"> 【' + data.memberinfo.nickname + '】</a>';
                }

                //放入进去上面空的地方
                $(".login-info").html(str);
            }
        })
    };
    function exitLogin() {
        $.ajax({
            url: "/exitLogin" ,
            type: "GET",
            success: function () {
                location.href="../index.html";
            }
        })
    };
    function showAll(){
        $.ajax({
            url: "/user/showAllGwc",
            type: "GET",
            success: function (data) {
                console.log(data);
                let str = '';
                totalPrice = 0;
                for (let i = 0; i < data.length; i++) {
                    let gwcid = data[i].gwcid;
                    totalPrice += (data[i].price * data[i].num);
                    str +=
                        `<tr class="goodsTr" id="goodsTr">
                    <td align="left" width="200px">
                    <span><img src="../file/images/goods/${data[i].pic}" width="100px" height="100px"/></span>
                    <span style="vertical-align: top">${data[i].gname}</span></td>

                    <td align="center" width="100">${data[i].price}</td>
                    <td align="center" width="70">
                    <a onclick="subOne(${data[i].gwcid})" style="cursor: pointer;font-size: 20px;">  -&nbsp;&nbsp;&nbsp;</a>
                    ${data[i].num}
                    <a onclick="addOne(${data[i].gwcid})" style="cursor: pointer;font-size: 20px;">&nbsp;&nbsp;&nbsp;+ </a>
                    <!--<a href="/user/addGwcGood?gwcid=${data[i].gwcid}" id="addOne"> + </a>--></td>
                    <td align="center" width="60">${data[i].price * data[i].num}</td>
                    <td align="center" width="100">
                    <a onclick="delGoods(${data[i].gwcid})" style="cursor: pointer">删除</a></td>
                </tr>`;
                }
                $("#totalPrice").html(totalPrice);
                $("#gwcTable").html(str);


                let str2 = '<a href="/user/ensurePay.html#'+totalPrice+'">结算</a>';
                $("#toPayDiv").html(str2);


                // $("#toPay").click(function (){
                //     location.href = "toPay.html";
                    // console.log(totalPrice);
                    // $.ajax({
                    //     url: "http://localhost:8080/toPay.html#" +totalPrice ,
                    //     type: "GET",
                        // success: function (data) {
                        //     console.log(data);
                        //     // location.href = "toPay.html";
                        // }
                //     })
                // });

                }
        })
        };
        function addOne(gwcid) {
            $.ajax({
                url: "/user/addGwcGood/" +gwcid ,
                type: "GET",
                success: function () {
                    showAll();
                }
            })
        };
        function subOne(gwcid) {
            $.ajax({
                url: "/user/subGwcGood/" +gwcid ,
                type: "GET",
                success: function () {
                    showAll();
                }
            })
        };
        function delGoods(gwcid) {
            var ifAdd = confirm("确定删除该商品吗？");
            if (!ifAdd) {
                return;
            }
            $.ajax({
                url: "/user/delGood/" +gwcid ,
                type: "GET",
                success: function () {
                    showAll();
                }
            })
        };
        function clearGoods() {
            var ifAdd = confirm("确定清空购物车吗？");
            if (!ifAdd) {
                return;
            }
            $.ajax({
                url: "/user/clearGwc" ,
                type: "GET",
                success: function () {
                    showAll();
                }
            })
        };






        // //把总额发送给后台 ->  后台发送给toPay页面
        // $("#toPay").click(function (){
        //     $.ajax({
        //         url: "http://localhost:8080/user/toPay/" +totalPrice ,
        //         type: "GET",
        //         success:function (data){
        //             if(data!=null){
        //                 location.href="toPay.html";
        //             }
        //         }
        //     })
        // });
</script>
</body>
</html>